package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.dao.TRefundRecordMapper;
import com.wsd.smartcarwasher.domain.CardOrder;
import com.wsd.smartcarwasher.domain.TRefundRecord;
import com.wsd.smartcarwasher.domain.TRefundRecordExample;
import com.wsd.smartcarwasher.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class RefundRecordService {

    @Autowired
    private TRefundRecordMapper refundRecordMapper;
    @Autowired
    private  CardOrderService cardOrderService;

    /**
     * 用户退款申请
     * @param orderSn
     */
    public void  userApplyRefund(String orderSn)
    {
        CardOrder cardOrder=this.cardOrderService.queryOrderInfoByOrderSn(orderSn);

        TRefundRecord tRefundRecord=new TRefundRecord();
        tRefundRecord.setCardid(cardOrder.getCardId());
        if(StringUtil.isNullOrEmpty(cardOrder.getHeuserId()))
        {
            tRefundRecord.setUserid(cardOrder.getUserId());
        }else{
            tRefundRecord.setUserid(cardOrder.getHeuserId());
        }
        tRefundRecord.setOrdersn(orderSn);
        tRefundRecord.setRefundstatus((byte) -1);
        tRefundRecord.setReqinfo(null);
        tRefundRecord.setStatus((byte)1);
        tRefundRecord.setCreatetime(new Date());
        this.refundRecordMapper.insert(tRefundRecord);
    }

    /**
     * 用户完成退款申请
     * @param orderSn
     */
    public void userApplyRefundFinish(String orderSn,String reqInfo)
    {
        TRefundRecord tRefundRecord=this.checkUserExistRefundApply(orderSn);
        if(!StringUtil.isNullOrEmpty(tRefundRecord)){
            TRefundRecordExample example=new TRefundRecordExample();
            example.createCriteria().andOrdersnEqualTo(orderSn)
            .andRefundstatusEqualTo((byte)-1);
            List<TRefundRecord> tRefundRecordList=this.refundRecordMapper.selectByExample(example);
            tRefundRecord=tRefundRecordList.get(0);
            tRefundRecord.setUpdatetime(new Date());
            tRefundRecord.setReqinfo(reqInfo);
            tRefundRecord.setRefundstatus((byte)1);
            this.refundRecordMapper.updateByPrimaryKeySelective(tRefundRecord);

        }

    }


    public TRefundRecord checkUserExistRefundApply(String orderSn)
    {
        TRefundRecordExample example=new TRefundRecordExample();
        example.createCriteria().andOrdersnEqualTo(orderSn)
                .andRefundstatusEqualTo((byte)-1);
        List<TRefundRecord> tRefundRecordList=this.refundRecordMapper.selectByExample(example);
        if(StringUtil.isNullOrEmpty(tRefundRecordList))
        {
            return null;
        }
        return tRefundRecordList.get(0);
    }

    public TRefundRecord hascheckUserExistRefundApply(String orderSn)
    {
        TRefundRecordExample example=new TRefundRecordExample();
        example.createCriteria().andOrdersnEqualTo(orderSn);
        List<TRefundRecord> tRefundRecordList=this.refundRecordMapper.selectByExample(example);
        if(StringUtil.isNullOrEmpty(tRefundRecordList))
        {
            return null;
        }
        return tRefundRecordList.get(0);
    }

}
